02 / 05

What are common sources of memory leaks in Go services?

The most common memory leaks in Go are goroutine leaks, unbounded caches, unclosed HTTP response bodies, and keeping references to large backing arrays via sub-slices.

Common memory leak sources
  1. 1

    Goroutine leaks: blocked goroutines hold their stack and all referenced heap objects alive forever

  2. 2

    Unbounded global maps or caches that only grow — add a size limit or TTL eviction

  3. 3

    time.After inside loops: each call creates a new channel and timer that leaks until it fires

  4. 4

    Unclosed HTTP response bodies: resp.Body.Close() must be called even when not reading the body

  5. 5

    Sub-slice keeping large array alive: a small slice of a large array prevents the whole array from being GC'd

  6. 6

    CGo retained pointers: C code holding Go pointers prevents GC collection

Common leak examples and fixes